﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace TextDungeon
{
    class Updater
    {
        public Position WorldPosition = new Position();
        public List<Entity> walls = new List<Entity>();
        public List<Creatures> mobs = new List<Creatures>();
        World world = new World(5, 5);
        GUI gui = new GUI();
        Position playerpos = new Position(2, 2);
        Player player;

        public Updater() 
        {
            player = new Player(gui.ByteCode(1));
            player.pos = playerpos;
            walls = world.RenderLevel(WorldPosition);

            
        }


        /// <summary>
        ///    0
        /// 3     1
        ///    2
        /// returns the position towards the number
        /// </summary>
        /// <param name="dir">direction</param>
        /// <param name="pos">origin</param>
        /// <param name="revert">if it should do backwards</param>
        /// <returns></returns>
        public Position Direction(byte dir, Position pos, bool revert = false) 
        {
            int addX = 0;
            int addY = 0;

            switch (dir) 
            {
                case 0:
                    addY = -1;
                    break;
                case 1:
                    addX = 1;
                    break;
                case 2:
                    addY = 1;
                    break;
                case 3:
                    addX = -1;
                    break;
            }

            if (revert)
            {
                addX *= -1;
                addY *= -1;
            }
            pos.X += addX;
            pos.Y += addY;

            return pos;
        }

        public ConsoleKey Input()
        {
            if (Console.KeyAvailable)
            {
                return Console.ReadKey(true).Key;
            }
            else
                return ConsoleKey.NoName;
        }
        /// <summary>
        /// goes through the entire wall entites and cheacks if the positon matches and returns a bool
        /// </summary>
        /// <param name="pos">targeted position</param>
        /// <returns></returns>
        public bool CollitionCheck(Position pos) 
        {
            bool a = false;
            for (int i = 0; i < walls.Count; i++)
            {
                var w = walls[i];
                if (w.pos.X == pos.X && w.pos.Y == pos.Y) 
                {
                    if (w.type != EntityType.Door) // to add an entity that dont have collition just add this line : && w.type != EntityType.#object type you want to be ingored#
                    {
                        
                        a = true;
                        
                    }
                }
            }
            return a;
        }
        public Position MoveToTowards(Creatures e, byte dir, Position pos) //made easy for moving objects at a direction 
        {
            pos = Direction(dir, pos);
            if (CollitionCheck(pos)) 
            {
                pos = Direction(dir, pos, true);
            }

            e.pos = pos;
            return pos;
        }
        public void ClearAt(Position pos) //this clears a object rendered on the screen and dont exist in entites or a moved entity
        {
            gui.WriteAt(gui.ByteCode(0), pos);
            foreach (var wall in walls)
            {
                if (wall.pos.X  == pos.X && wall.pos.Y == pos.Y) 
                {
                    gui.WriteAt(wall.symbol, wall.pos);
                }                
            }
            
        }
        
        public void MainLoop()
        {
              
            bool on = true;
            var key = ConsoleKey.NoName;
            
            while (on) // the only thing in here right now is player movment, next up is ai movment.... dear god
            {
                gui.WriteAt(player.symbol, playerpos, ConsoleColor.Red);
                key = Input();
                if (key != ConsoleKey.NoName) 
                {
                    if (key == ConsoleKey.D) 
                    {
                        ClearAt(playerpos);
                        playerpos = MoveToTowards(player, 1, playerpos);
                    }
                    else if (key == ConsoleKey.S) 
                    {
                        ClearAt(playerpos);
                        playerpos = MoveToTowards(player, 2, playerpos);
                    }
                    else if (key == ConsoleKey.W) 
                    {
                        ClearAt(playerpos);
                        playerpos = MoveToTowards(player, 0, playerpos);
                    }
                    else if (key == ConsoleKey.A) 
                    {
                        ClearAt(playerpos);
                        playerpos = MoveToTowards(player, 3, playerpos);
                    }
                    else if (key == ConsoleKey.Spacebar) 
                    {
                        Console.Clear();
                        world.RenderLevel(WorldPosition);
                        
                    }
                    player.pos = playerpos;
                }
                //all player based actions and render logic in here 
                
            }


        }
    }
}

